<template>
  <div class="Awareness">
    <div class="title">
      <p class="tip">人行态势感知</p>
      <button class="goBack" @click="back">返回</button>
    </div>
    <div class="contain">
      <el-container>
        <el-header>
          <el-menu
            :default-active="activeIndex"
            class="el-menu-demo"
            mode="horizontal"
            @select="handleSelect"
          >
            <el-menu-item index="1">安全通知事件-今日通知</el-menu-item>
            <el-menu-item index="2">运行工单-超时未处理</el-menu-item>
            <el-menu-item index="3">运行工单-当前处理</el-menu-item>
            <el-menu-item index="4">例行性演练-当前处理</el-menu-item>
          </el-menu>
        </el-header>
        <el-main>
          <!-- <router-view></router-view> -->
          <div class="main">
            <el-form ref="searchForm" :inline="true" class="demo-form-inline">
              <el-form-item label="时间">
                <el-date-picker
                  v-model="value1"
                  type="datetimerange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  value-format="YYYYMMDDHHmmss"
                  @change="change(value1)"
                >
                </el-date-picker>
              </el-form-item>
              <el-form-item label="事件状态" v-if="activeIndex == '1' ? false : true">
                <el-select
                  clearable
                  v-model="status"
                  placeholder="请选择状态"
                  @change="handleSelect_status"
                >
                  <el-option
                    v-for="(item, index) in statusList"
                    :key="index + 1"
                    :label="item.valueDetail"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="单号" v-if="activeIndex == '1' ? false : true">
                <el-input
                  clearable
                  v-model="changeCode"
                  class="inline-input"
                  placeholder="请输入单号"
                />
              </el-form-item>
            </el-form>
            <div class="button">
              <button @click="detail" class="btn" v-if="activeIndex == '1' ? false : true">
                查看详情
              </button>
              <button @click="search" @keyup.enter="monitor()" class="btn reset"> 搜索 </button>
              <button @click="resetForm" class="btn">重置</button>
              <el-dropdown trigger="click">
                <span class="el-dropdown-link">
                  导出<i class="el-icon-arrow-down el-icon--right"></i>
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item icon="el-icon-plus" @click="downloadExcel('currentPage')">
                      导出当前页</el-dropdown-item
                    >
                    <el-dropdown-item
                      icon="el-icon-circle-plus"
                      @click="Select_downloadExcel('all')"
                    >
                      导出所有页</el-dropdown-item
                    >
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </div>
          </div>
          <div class="tableBox">
            <div class="desc">
              <img src="../../assets/images/bbsm.png" alt="" />
              {{ desc }}
            </div>
            <div class="table" ref="container">
              <el-table
                :height="tableHeight"
                ref="multipleTableRef"
                :data="changeList"
                tooltip-effect="dark"
                row-key="getRowKey"
                v-if="activeIndex == '1' ? true : false"
              >
                <!--  -->
                <el-table-column label="序号" width="100" type="index" />
                <el-table-column
                  align="center"
                  label="报警时间"
                  prop="smsTime"
                  show-overflow-tooltip
                >
                </el-table-column>
                <el-table-column
                  align="center"
                  prop="smsText"
                  label="报警内容"
                  show-overflow-tooltip
                >
                </el-table-column>
              </el-table>
              <el-table
                :height="tableHeight"
                ref="multipleTableRef"
                :data="changeList"
                tooltip-effect="dark"
                @select="selectChange"
                @selection-change="handleSelectionChange"
                row-key="getRowKey"
                @row-click="handleRowClick"
                v-if="activeIndex == '1' ? false : true"
              >
                <!--  -->
                <el-table-column
                  align="center"
                  :reserve-selection="false"
                  type="selection"
                ></el-table-column>
                <el-table-column label="序号" width="100" type="index" />
                <el-table-column align="center" label="单号" prop="eventNo" show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                  align="center"
                  prop="eventEntityName"
                  label="告警对象"
                  show-overflow-tooltip
                >
                </el-table-column>
                <el-table-column
                  align="center"
                  prop="createTime"
                  label="创建时间"
                  show-overflow-tooltip
                >
                </el-table-column>
                <el-table-column
                  align="center"
                  prop="lastTime"
                  label="持续时间"
                  show-overflow-tooltip
                >
                </el-table-column>
                <el-table-column
                  align="center"
                  prop="handlePerson"
                  label="当前处理人"
                  show-overflow-tooltip
                >
                </el-table-column>
                <el-table-column
                  align="center"
                  prop="handlePersonDept"
                  label="当前责任处室"
                  show-overflow-tooltip
                >
                </el-table-column>
                <el-table-column
                  align="center"
                  prop="eventDescr"
                  label="详细描述"
                  show-overflow-tooltip
                >
                </el-table-column>
                <el-table-column
                  align="center"
                  prop="handleDescr"
                  label="处理过程描述"
                  show-overflow-tooltip
                >
                </el-table-column>
                <el-table-column
                  align="center"
                  prop="status"
                  label="事件状态"
                  show-overflow-tooltip
                >
                </el-table-column>
              </el-table>
            </div>
            <!-- 分页 -->
            <el-pagination
              ref="pagination"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="pageNum"
              :page-sizes="[10, 15, 20]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
            ></el-pagination>
          </div>
        </el-main>
      </el-container>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { ElMessage } from 'element-plus';
  import { ref, reactive, onMounted } from 'vue';
  import {
    selectByFmSawoSmsQuery,
    exportFmSawoSmsList,
    download,
    feelHandleEventCountList,
    feelHandleEventCountExport
  } from '@/http/api/Situational';
  import { paraType } from '@/http/api1';
  import { useRoute, useRouter } from 'vue-router';
  const router = useRouter();
  const route = useRoute();
  const changeList = reactive([]);
  const pageNum = ref(1);
  const pageSize = ref(10);
  const total = ref(0);
  const value1 = ref('');
  const startTime = ref('');
  const endTime = ref('');
  const changeCode = ref('');
  const status = ref(''); // 事件状态
  const statusCode = ref(''); // 事件状态参数码
  const statusList = reactive([]);
  const tableHeight = ref(0);
  const container = ref(null);
  const multipleTableRef = ref<InstanceType<typeof ElTable>>();
  const multipleTable = ref<User[]>([]);
  const activeIndex = ref(route.params.type ? route.params.type : '1');
  const type = ref('');
  const desc = ref('');
  onMounted(() => {
    calHeight();
    Event_status();
    switch (activeIndex.value) {
      case '1':
        getList();
        desc.value = '安全通知事件-今日通知：时间范围今日0点-24点之间的人行态势感知通知类消息总和';
        break;
      case '2':
        type.value = '1';
        desc.value = '运行工单-超时未处理：当前事件单状态非“关闭”，持续时间超过“24h”';
        HandleEventCountList();
        break;
      case '3':
        type.value = '2';
        desc.value = '运行工单-当前处理：当前事件单状态非“关闭”，持续时间未超过“24h”';
        HandleEventCountList();
        break;
      case '4':
        type.value = '3';
        desc.value =
          '例行性演练-当前处理：告警对象“人行态势感知”，详细描述包含“业务连续性压力测试”';
        HandleEventCountList();
        break;
      default:
        break;
    }
  });
  const change = (value) => {
    startTime.value = value1.value ? value[0] : '';
    endTime.value = value1.value ? value[1] : '';
  };
  const calHeight = () => {
    const rect = container.value.getBoundingClientRect();
    tableHeight.value = rect.height;
  };
  const HandleEventCountList = () => {
    feelHandleEventCountList({
      type: type.value,
      pageNum: JSON.stringify(pageNum.value),
      pageSize: JSON.stringify(pageSize.value),
      createStartTime: startTime.value,
      createEndTime: endTime.value,
      status: statusCode.value,
      eventNo: changeCode.value
    }).then((res) => {
      if ((res as any).data.code !== 0) {
        ElMessage({
          message: (res as any).data.msg,
          type: 'error'
        });
        return;
      }
      changeList.length = 0;
      (changeList as any).push(...res.data.data.rows);
      total.value = res.data.data.total;
    });
  };
  const getList = () => {
    selectByFmSawoSmsQuery({
      pageNum: JSON.stringify(pageNum.value),
      pageSize: JSON.stringify(pageSize.value),
      startTime: startTime.value,
      endTime: endTime.value
    }).then((res) => {
      if ((res as any).data.code !== 0) {
        ElMessage({
          message: (res as any).data.msg,
          type: 'error'
        });
        return;
      }
      changeList.length = 0;
      (changeList as any).push(...res.data.data.rows);
      total.value = res.data.data.total;
    });
  };
  const Event_status = () => {
    paraType({
      type: 'Event_status',
      pageNum: 1,
      pageSize: 20
    }).then((res) => {
      if ((res as any).data.code !== 0) {
        ElMessage({
          message: (res as any).data.msg,
          type: 'error'
        });
        return;
      }
      statusList.length = 0;
      res.data.data.rows.forEach((item) => {
        (statusList as any).push({
          valueDetail: item.valueDetail,
          value: item.value
        });
      });
    });
  };
  const search = () => {
    changeCode.value = changeCode.value.replace(/(^\s*)|(\s*$)/g, '');
    switch (activeIndex.value) {
      case '1':
        getList();
        break;
      case '2':
        type.value = '1';
        HandleEventCountList();
        break;
      case '3':
        type.value = '2';
        HandleEventCountList();
        break;
      case '4':
        type.value = '3';
        HandleEventCountList();
        break;
      default:
        break;
    }
  };
  const resetForm = () => {
    value1.value = '';
    startTime.value = '';
    endTime.value = '';
    pageNum.value = 1;
    changeCode.value = '';
    statusCode.value = '';
    status.value = '';
  };
  const HandleEventCountExport = (val: any) => {
    feelHandleEventCountExport({
      type: type.value,
      createStartTime: startTime.value,
      createEndTime: endTime.value,
      status: statusCode.value,
      eventNo: changeCode.value,
      pageNum: JSON.stringify(pageNum.value),
      pageSize: JSON.stringify(pageSize.value),
      currentPage: val
    }).then((res) => {
      if ((res as any).data.code !== 0) {
        ElMessage({
          message: (res as any).data.msg,
          type: 'error'
        });
        return;
      }
      downloadlist(res.data.msg);
    });
  };
  const FmSawoSmsList = (val: any) => {
    exportFmSawoSmsList({
      pageNum: JSON.stringify(pageNum.value),
      pageSize: JSON.stringify(pageSize.value),
      startTime: startTime.value,
      endTime: endTime.value,
      currentPage: val
    }).then((res) => {
      if ((res as any).data.code !== 0) {
        ElMessage({
          message: (res as any).data.msg,
          type: 'error'
        });
        return;
      }
      downloadlist(res.data.msg);
    });
  };
  // 导出当前页
  const downloadExcel = (data: any) => {
    switch (activeIndex.value) {
      case '1':
        FmSawoSmsList(data);
        break;
      case '2':
        type.value = '1';
        HandleEventCountExport(data);
        break;
      case '3':
        type.value = '2';
        HandleEventCountExport(data);
        break;
      case '4':
        type.value = '3';
        HandleEventCountExport(data);
        break;
      default:
        break;
    }
  };
  const downloadlist = (type: string) => {
    download({
      fileName: type,
      delete: true
    }).then((res) => {
      const link = document.createElement('a');
      let blob = new Blob([res.data], {
        type: 'application/vnd.ms-excel'
      });
      link.style.display = 'none';
      link.href = URL.createObjectURL(blob);
      link.download = type; //下载的文件名
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    });
  };
  const Select_downloadExcel = (data: any) => {
    switch (activeIndex.value) {
      case '1':
        FmSawoSmsList('');
        break;
      case '2':
        type.value = '1';
        HandleEventCountExport(data);
        break;
      case '3':
        type.value = '2';
        HandleEventCountExport(data);
        break;
      case '4':
        type.value = '3';
        HandleEventCountExport(data);
        break;
      default:
        break;
    }
  };
  const handleSelect = (key: any) => {
    pageNum.value = 1;
    activeIndex.value = key;
    switch (activeIndex.value) {
      case '1':
        getList();
        desc.value = '安全通知事件-今日通知：时间范围今日0点-24点之间的人行态势感知通知类消息总和';
        break;
      case '2':
        type.value = '1';
        desc.value = '运行工单-超时未处理：当前事件单状态非“关闭”，持续时间超过“24h”';
        HandleEventCountList();
        break;
      case '3':
        type.value = '2';
        desc.value = '运行工单-当前处理：当前事件单状态非“关闭”，持续时间未超过“24h”';
        HandleEventCountList();
        break;
      case '4':
        type.value = '3';
        desc.value =
          '例行性演练-当前处理：告警对象“人行态势感知”，详细描述包含“业务连续性压力测试”';
        HandleEventCountList();
        break;
      default:
        break;
    }
  };
  const handleSelect_status = (item) => {
    statusCode.value = item;
  };
  const handleSizeChange = (size) => {
    pageSize.value = size;
    switch (activeIndex.value) {
      case '1':
        getList();
        desc.value = '安全通知事件-今日通知：时间范围今日0点-24点之间的人行态势感知通知类消息总和';
        break;
      case '2':
        type.value = '1';
        desc.value = '运行工单-超时未处理：当前事件单状态非“关闭”，持续时间超过“24h”';
        HandleEventCountList();
        break;
      case '3':
        type.value = '2';
        desc.value = '运行工单-当前处理：当前事件单状态非“关闭”，持续时间未超过“24h”';
        HandleEventCountList();
        break;
      case '4':
        type.value = '3';
        desc.value =
          '例行性演练-当前处理：告警对象“人行态势感知”，详细描述包含“业务连续性压力测试”';
        HandleEventCountList();
        break;
      default:
        break;
    }
  };
  const handleCurrentChange = (num) => {
    pageNum.value = num;
    switch (activeIndex.value) {
      case '1':
        getList();
        desc.value = '安全通知事件-今日通知：时间范围今日0点-24点之间的人行态势感知通知类消息总和';
        break;
      case '2':
        type.value = '1';
        desc.value = '运行工单-超时未处理：当前事件单状态非“关闭”，持续时间超过“24h”';
        HandleEventCountList();
        break;
      case '3':
        type.value = '2';
        desc.value = '运行工单-当前处理：当前事件单状态非“关闭”，持续时间未超过“24h”';
        HandleEventCountList();
        break;
      case '4':
        type.value = '3';
        desc.value =
          '例行性演练-当前处理：告警对象“人行态势感知”，详细描述包含“业务连续性压力测试”';
        HandleEventCountList();
        break;
      default:
        break;
    }
  };
  // 点击行触发，选中或不选中复选框
  const handleRowClick = (row) => {
    multipleTable.value = row;
    multipleTableRef.value.toggleRowSelection(row);
    selectChange(multipleTable.value);
  };
  const selectChange = (selection: string) => {
    if (selection.length > 1) {
      const del_row = selection.shift();
      multipleTableRef.value!.toggleRowSelection(del_row);
    }
  };
  const handleSelectionChange = (val: string) => {
    multipleTable.value = val;
  };
  const detail = () => {
    if (!multipleTable.value.length) {
      ElMessage({
        message: '请先勾选查看的数据',
        type: 'warning'
      });
    } else {
      router.push({
        name: 'monitor_detail',
        params: {
          value: activeIndex.value,
          type: multipleTable.value[0].eventId
        }
      });
    }
  };
  const back = () => {
    // this.$router.go(-1);
    router.push({
      name: 'restructure'
    });
  };
</script>
<style lang="less">
  .Awareness {
    width: 100%;
    height: 100vh;
    background: #eaeef4;
    padding: 20px;
    box-sizing: border-box;
    //background-image: url("../../assets/images/background-image.jpg");
    .title {
      display: flex;
      justify-content: space-between;
      margin-bottom: 15px;
      .tip {
        height: 34px;
        line-height: 34px;
        font-size: 18px;
        font-weight: 400;
        color: #46545d;
        margin: 0;
        padding: 0;
      }

      .goBack {
        background: #007af5;
        min-width: 80px;
        height: 34px;
        border: none;
        color: #fff;
        font-size: 12px;
        font-weight: 400;
        cursor: pointer;
      }
    }
    .contain {
      background: #fff;
      height: calc(100% - 50px);
      .el-container {
        height: 100%;
        .el-header {
          .el-menu--horizontal {
            border-bottom: none;
            .el-menu-item {
              font-size: 16px;
              font-weight: 400;
              color: #46545d;
              &.is-active {
                font-size: 16px;
                font-weight: bold;
                color: #007af5;
                border-bottom: 2px solid #007af5;
              }
            }
          }
        }
        .el-main {
          .main {
            display: flex;
            justify-content: space-between;
            margin-bottom: 15px;
            .demo-form-inline {
              :deep(input::placeholder) {
                font-size: 14px;
                font-weight: 400;
                color: #bbbbbb;
              }
              .el-form-item {
                margin-bottom: 0;
                line-height: 34px;
                height: 34px;
                // width: 40%;
              }
              :deep(.el-form-item__content) {
                line-height: 34px;
                //height: 43px;
                width: 80%;
              }
              :deep(.el-input__inner) {
                // width: 200px;
                // height: 34px;
                background: #ffffff;
                // border: 1px solid #dddddd;
                border-radius: 2px;
              }
              :deep(.el-form-item__label) {
                // color: #6fd7f7;
                font-size: 14px;
                font-weight: 400;
                color: #46545d;
                // height: 34px;
                // line-height: 34px;
              }
            }
            .button {
              display: flex;
              justify-content: space-around;
              // max-width: 40%;
              .btn {
                min-width: 80px;
                height: 34px;
                line-height: 34px;
                background: #007af5;
                border-radius: 2px;
                font-size: 12px;
                font-weight: 400;
                color: #ffffff;
                border: 0;
                cursor: pointer;
                &.reset {
                  margin: 0 10px;
                }
              }
              #goBack {
                // max-width: 131px;
                height: 34px;
                background: #007af5;
                border-radius: 2px;
                border: 0;
                font-size: 12px;
                font-weight: 400;
                color: #ffffff;
                cursor: pointer;
              }
              .el-dropdown {
                margin: 0px 10px;
                cursor: pointer;
              }
              .el-dropdown-link {
                // display: inline-block;
                width: 80px;
                height: 34px;
                line-height: 34px;
                text-align: center;
                color: #fff;
                background: #007af5;
                border-radius: 2px;
              }
            }
          }

          .tableBox {
            height: calc(100% - 50px);
            background: #fff;
            // padding: 20px;
            box-sizing: border-box;
            //overflow: hidden;
            .desc {
              font-size: 12px;
              font-weight: 400;
              color: #ec4b5a;
              background: #fbdbde;
              line-height: 33px;
              height: 33px;
              text-align: center;
              border-radius: 2px;
              display: flex;
              align-items: center;
              justify-content: center;
              img {
                width: 16px;
                height: 16px;
                // margin: auto;
                margin-right: 10px;
              }
            }
            .table {
              // background: rgba(0, 0, 0, 0.1);
              height: calc(100% - 93px);
              width: 100%;
              margin-bottom: 15px;
              :deep(.el-table-column--selection.is-leaf .el-checkbox) {
                display: none;
              }
              :deep(.el-table__cell.is-leaf) {
                //background: rgba(0, 0, 0, 0.1);
                // color: #6fd7f7;
                //color: #fff;
                color: #929ea8;
              }

              :deep(tr:hover > td) {
                background-color: transparent;
              }
              :deep(.el-table__empty-text) {
                //color: #fff;
                color: #929ea8;
              }
              :deep(td.el-table__cell) {
                //color: #fff;
                color: #929ea8;
              }
            }
            .el-pagination {
              height: 34px;
              padding: 0;
              box-sizing: border-box;
              //width: 50%;
              :deep(.el-pagination__total) {
                font-size: 14px;
                font-weight: 400;
                color: #bbbbbb;
                line-height: 34px;
              }

              :deep(.el-input__inner) {
                height: 34px;
                //width: 60px;
                background: #fff;
                //color: #66b1ff;
                color: #46545d;
              }
              :deep(button:disabled) {
                background: #fff;
                height: 34px;
                line-height: 34px;
                border: 1px solid #dddddd;
                //color: #66b1ff;
                color: #46545d;
                box-sizing: border-box;
              }
              :deep(.el-pager li) {
                //background: rgba(0, 0, 0, 0.1);
                background: #fff;
                height: 34px;
                line-height: 34px;
                border: 1px solid #dddddd;
                //color: #66b1ff;
                color: #46545d;
                box-sizing: border-box;
                &.active {
                  // color: #fff;
                  color: #46545d;
                }
              }
              :deep(.btn-prev) {
                background: #fff;
                height: 34px;
                line-height: 34px;
                border: 1px solid #dddddd;
                //color: #66b1ff;
                color: #46545d;
                box-sizing: border-box;
              }
              :deep(.btn-next) {
                background: #fff;
                height: 34px;
                line-height: 34px;
                border: 1px solid #dddddd;
                //color: #66b1ff;
                color: #46545d;
                box-sizing: border-box;
              }
              :deep(span:not([class*='suffix'])) {
                color: #bbbbbb;
              }
              .el-pagination__jump {
                height: 34px;
                line-height: 34px;
              }
            }
          }
        }
      }
    }
  }
</style>
